package codingforgreat.class17;


//每一行有序，每一列有序求某个值是否存在的二维矩阵
public class Class01_FindNumInSortedMatrix {
    public static boolean isContains(int[][] matrix, int K) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return false;
        }
        int N = matrix.length;
        int M = matrix[0].length;
        int i = 0;
        int j = M - 1;
        while( i < N && j >= 0) {
                if(matrix[i][j] < K){
                    i++;
                }else
                if(matrix[i][j] > K){
                    j--;
                }else
                if(matrix[i][j] == K) {
                    return true;
                }
        }
        return false;
    }
    public static void main(String[] args) {
        int[][] matrix = new int[][] { { 0, 1, 2, 3, 4, 5, 6 }, // 0
                { 10, 12, 13, 15, 16, 17, 18 }, // 1
                { 23, 24, 25, 26, 27, 28, 29 }, // 2
                { 44, 45, 46, 47, 48, 49, 50 }, // 3
                { 65, 66, 67, 68, 69, 70, 71 }, // 4
                { 96, 97, 98, 99, 100, 111, 122 }, // 5
                { 166, 176, 186, 187, 190, 195, 200 }, // 6
                { 233, 243, 321, 341, 356, 370, 380 } // 7
        };
        int K = 233;
        System.out.println(isContains(matrix, K));
    }

}
